package cn.com.dhc.danlu.activity.home.wrap;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.base.WebViewActivity;
import cn.com.dhc.danlu.activity.home.viewmodule.AdvertisementHeader;
import cn.com.dhc.danlu.activity.home.viewmodule.SelectTypeHeader;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.util.UrlUtil;
import cn.com.dhc.danlu.widget.banner.AutoBannerPager;
import cn.com.hd.mall.web.webservices.entity.response.common.GetBannerListResponse;
import cn.com.hd.mall.web.webservices.entity.response.common.GetTopicListResponse;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-08-04
 * Time: 10:38
 * Description: 封装了首页轮播视图、type选择视图、广告条
 */
public class HomeViewModule {
    private AdvertisementHeader mAdvertisementHeader;
    //  2016/8/4 对宿主activity暴露 更新三个视图方法
    private AutoBannerPager mAutoBannerPager;
    private SelectTypeHeader mSelectTypeHeader;

    public HomeViewModule(@NonNull View parentView) {
        mAutoBannerPager = (AutoBannerPager) parentView.findViewById(R.id.abh_banner);
        mSelectTypeHeader = (SelectTypeHeader) parentView.findViewById(R.id.sth_filter);
        mAdvertisementHeader = (AdvertisementHeader) parentView.findViewById(R.id.ah_advertisement);
    }

    /**
     * 刷新轮播视图
     */
    public void refreshBanner(final List<GetBannerListResponse.BannerListModel> pics) {
        if (null == pics) {
            return;
        }
        List<String> imgDatas = new ArrayList<>();
        for (int i = 0; i < pics.size(); i++) {
            imgDatas.add(pics.get(i).getPicUrl());
        }
        mAutoBannerPager.refreshBanner(imgDatas);
        mAutoBannerPager.setPageClickListener(new AutoBannerPager.OnPageClickListener() {
            @Override
            public void onPageClicked(int pos) {
                GetBannerListResponse.BannerListModel model = pics.get(pos);
                if (!TextUtils.isEmpty(model.getLinkUrl())) {
                    WebViewActivity.startSelf(getContext(), CommonUtils.getStringByResId(R.string.activity_center), UrlUtil.getActivityUrl(model.getLinkUrl()), false);
                }
            }
        });
    }

    public Context getContext() {
        return mAdvertisementHeader.getContext();
    }

    /**
     * 刷新 类型视图
     */
    public void refreshTypeSource(List<GetTopicListResponse.TopicListModel> list) {
        if (null != list) {
            mSelectTypeHeader.refreshGridItem(list);
        }
    }

    /**
     * 刷新角标
     */
    public void refreshTypeBadge(boolean isOrderBadge, int count) {
        mSelectTypeHeader.refreshBadge(isOrderBadge, count);
    }

    /**
     * 刷新广告视图
     */
    public void refreshAdvertisment(GetBannerListResponse.BannerListModel model) {
        mAdvertisementHeader.refreshImgeUrl(model);
    }
}